package com.hui.controller;

import com.github.pagehelper.PageInfo;
import com.hui.entity.ResponseResult;
import com.hui.entity.Role;
import com.hui.entity.User;
import com.hui.entity.UserVo;
import com.hui.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/08/07/16:13
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 分页获取用户数据&条件查询用户数据
    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVo userVo) {
        PageInfo<User> pageInfo = userService.getUserByCondition(userVo);
        return new ResponseResult(true, 200, "查询成功", pageInfo);
    }

    // 修改用户状态
    @RequestMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(Integer id, String status) {
        // 传过来的状态是用户当前的状态，需要转换一下
        if ("ENABLE".equalsIgnoreCase(status)) {
            status = "DISABLE";
        } else {
            status = "ENABLE";
        }
        userService.updateUserStatus(id, status);
        return new ResponseResult(true, 200, "查询成功", status);
    }


    //获取用户拥有的角色
    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRoleById(Integer id) {
        List<Role> list = userService.getUserRoleById(id);
        return new ResponseResult(true, 200, "查询成功", list);
    }

    // 分配角色
    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVo userVo) {
        userService.userContextRole(userVo);
        return new ResponseResult(true, 200, "分配角色成功", null);
    }

    // 用户登入
    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) throws Exception {
        user = userService.login(user);
        if (user == null) {
            return new ResponseResult(false, 400, "登入失败", null);
        } else {
            // 将 user_id  与  access_token 保存到session中
            String access_token = UUID.randomUUID().toString(); // 随机生产
            HttpSession session = request.getSession();
            session.setAttribute("user_id", user.getId());
            session.setAttribute("access_token", access_token);
            // 将 user_id、access_token、user 返回给前端
            Map<String, Object> map = new HashMap<>();
            map.put("user_id", user.getId());
            map.put("access_token", access_token);
            map.put("user", user);
            return new ResponseResult(true, 1, "登入成功", map);
        }
    }

    // 获取用户拥有的权限
    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request) {
        // 获取请求头中的token
        String head_token = request.getHeader("Authorization");
        // 获取session中的token
        String session_token = (String) request.getSession().getAttribute("access_token");
        // 如果token一致，表示该用户以登入，就获取session中的userId
        if (head_token.equals(session_token)) {
            Integer userId = (Integer) request.getSession().getAttribute("user_id");
            Map<String, Object> map = userService.getUserPermissions(userId);
            return new ResponseResult(true, 200, "获取权限信息成功", map);
        } else {
            return new ResponseResult(false, 400, "获取权限信息失败", null);
        }
    }

}
